<?php

declare(strict_types=1);
/**
 * 作者：薛原
 * 功能：用户认证控制器
 * 时间：2025/10/17
 */

namespace App\Controller\v1;

use App\Service\SignInService;
use App\Service\UserArticleService;
use App\Service\UserProfilesService;
use App\Service\UserService1;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Middleware;

#[Controller(prefix: "/api/v1/auth")]

//token验证中间件
#[Middleware(\App\Middleware\TokenMiddleware::class)]
class AuthController extends AbstractController
{
    #[Inject]
    private UserService1 $userService;

    #[Inject]
    private SignInService  $signInService;

    #[Inject]
    private UserArticleService $userArticleService;

    #[Inject]
    private UserProfilesService $userProfilesService;

    /**
     * 用户注册接口
     * @param RequestInterface $request
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    #[PostMapping("register")]
    public function register(RequestInterface $request): array|\Psr\Http\Message\ResponseInterface
    {
        $params = $this->parseParams($request);

        $name = $params['name'];
        $email = $params['email'];
        $password = $params['password'];
        $password_confirmation = $params['password_confirmation'];

        $userObj = [];
        $userObj['name'] = $name;
        $userObj['email'] = $email;
        $userObj['password'] = $password;
        $userObj['password_confirmation'] = $password_confirmation;
        try{
            $ret = $this->userService->createUser($userObj);
        }catch (\App\Exception\BusinessException $e){
            return $this->fail(["msg"=>"注册失败","errors"=>json_decode($e->getMessage())]);
        }
        return $this->success($ret,"注册成功");
    }

    /**
     * 用户登录接口
     * @param RequestInterface $request
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    #[PostMapping("login")]
    public function login(RequestInterface $request)
    {
        $params = $this->parseParams($request);
        $email = $params['email'];
        $password = $params["password"];
        $server_info = $request->getServerParams();
        $ip = $server_info['remote_addr'];
        try{
            $ret = $this->userService->login($email,$password,$ip);
        }catch (\App\Exception\BusinessException $e){
            return $this->fail(40002,"登录失败",["errors"=>$e->getMessage()]);
        }
        return $this->success($ret,"登录成功");
    }

    /**
     * 用户个人信息获取接口
     * @param RequestInterface $request
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    #[GetMapping("me")]
    public function me(RequestInterface $request)
    {
        $user_id = Context::get("login_user_id")."";
        try{
            $user_data = $this->userService->getUserById($user_id,true);
            unset($user_data["password"]);
            $user_article_data = $this->userArticleService->getUserStatsData($user_id);
            $ret = $user_data;
            $ret['stat'] = $user_article_data;

            $ret['today_is_sign'] = $this->signInService->getDayIsSign((int) $user_id);

        }catch (\App\Exception\BusinessException $e){
            return $this->fail(40002,"请求失败",["errors"=>$e->getMessage()]);
        }
        return $this->success($ret,"success");
    }

}
